package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.model.map.City;
import com.cljz.makepassenger.ui.holder.MapAddressListHolder;

import java.util.List;

/**
 * Created by apple on 2017/4/7.
 */

public class MapAddressListAdapter extends RecyclerView.Adapter<MapAddressListHolder> {
    public void setSuggestionInfoList(List<City> suggestionInfoList) {
        this.suggestionInfoList = suggestionInfoList;
        notifyDataSetChanged();
    }

    private List<City> suggestionInfoList;
    private Context context;
    private OnItemClickListener mOnItemClickListener;

    public MapAddressListAdapter(Context context, List<City> suggestionInfoList) {
        this.suggestionInfoList = suggestionInfoList;
        this.context = context;
    }

    @Override
    public MapAddressListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MapAddressListHolder(View.inflate(context, R.layout.item_addr_layout, null));
    }

    @Override
    public void onBindViewHolder(MapAddressListHolder holder, final int position) {
        holder.setData(suggestionInfoList, position);
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(suggestionInfoList.get(position));
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(suggestionInfoList.get(position));
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return suggestionInfoList == null ? 0 : suggestionInfoList.size();
    }


    public interface OnItemClickListener {
        void onClick(City city);

        void onLongClick(City city);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
